﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BKH.BLL;
namespace BKH.MyUserControl.UCHienThi
{
    public partial class ucNhaCungCap : UserControl
    {
        public ucNhaCungCap()
        {
            InitializeComponent();
            ShowGrid();
            cậpNhậtToolStripMenuItem.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            tbl_NhaCungCap cungcap = new tbl_NhaCungCap();
            NhaCungCapBUO ncc = new NhaCungCapBUO();
           
            cungcap.Name = txtNhaCungCap.Text;
            cungcap.Address = txtDiaChi.Text;
            cungcap.Tel = txtTel.Text;
            if (ncc.Insert(cungcap))
            {
                MessageBox.Show("Thêm thành công");
            }
            else
            {
                MessageBox.Show("Thất bại");
            }

            ShowGrid();
        }
        void ShowGrid()
        {

            NhaCungCapBUO ncc = new NhaCungCapBUO ();
            dataGridView1.DataSource = ncc.GetAll();

        }
        private String ID_Co;
        tbl_NhaCungCap nhacungcap = new tbl_NhaCungCap();

        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
           
        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                NhaCungCapBUO ncc = new NhaCungCapBUO ();
                if (ncc.Delete(int.Parse(ID_Co)))
                {
                    MessageBox.Show("Xóa thành công");

                }
                else
                {
                    MessageBox.Show("Xóa thất bại");
                }
            }
            catch (Exception)
            {

                MessageBox.Show("Xóa thất bại");
            }
            ShowGrid();
        }

        private void sửaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            cậpNhậtToolStripMenuItem.Enabled = true;
        }

        private void cậpNhậtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                NhaCungCapBUO ncc = new NhaCungCapBUO();
                if(ncc.Update(int.Parse(ID_Co),nhacungcap))
                {
                      MessageBox.Show("Cập nhật thành công");
                }
                else
                {
                    MessageBox.Show("Cập nhật thất bại");
                }
            }
            catch (Exception)
            {

                MessageBox.Show("Cập nhật thất bại");
            }
            ShowGrid();
            cậpNhậtToolStripMenuItem.Enabled = false;

        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                // Only RowHeader and Cells (which make up a row) should fire a menu
                if (hit.Type == DataGridViewHitTestType.RowHeader
                    || hit.Type == DataGridViewHitTestType.Cell
                    && hit.RowIndex >= 0)
                {
                    // Notice how I assign the menu to the dataGridView below...

                    dataGridView1.ClearSelection();
                    dataGridView1.Rows[hit.RowIndex].Selected = true;
                    dataGridView1.ContextMenuStrip = contextMenuStrip1;
                    dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));


                    ID_Co = dataGridView1.Rows[hit.RowIndex].Cells[0].Value.ToString();
                    nhacungcap.Name = dataGridView1.Rows[hit.RowIndex].Cells[1].Value.ToString();
                    nhacungcap.Address = dataGridView1.Rows[hit.RowIndex].Cells[2].Value.ToString();
                    nhacungcap.Tel = dataGridView1.Rows[hit.RowIndex].Cells[3].Value.ToString();

                }

                // and then I remove the menu here...
                // This is because after we assign it to the dataGridView
                // it allows you to right-click anywhere on the control to
                // get a menu.  Try removing this line and expirement.
                dataGridView1.ContextMenuStrip = null;




            }
        }
    }
}
